/**
 * 
 */
package com.kebo2.client;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.view.client.ListDataProvider;

/**
 * @author Tim
 *This class shows the result after a search query
 */
public class ShowAndParseResult {
	
	private VerticalPanel vp;
	private Document messageDom;
	private CellTable<Media> cellTable;
	private Column<Media, String> titleColumn;
	private Column<Media, String> authorColumn;
	private Column<Media, String> author2Column;
	private Column<Media, String> genreColumn;
	private TextColumn<Media> theme1Column;
	private TextColumn<Media> theme2Column;
	private TextColumn<Media> ageColumn;
	private TextColumn<Media> priceColumn;
	private TextColumn<Media> placeColumn;
	private SimplePager simplePager;
	private static List<Media> mediaList;
	private static Media[] mediaArray;
	
	public ShowAndParseResult(boolean currentMode, String result) {
		Parser object = new Parser();
		messageDom = object.parseXml(result);
		
		if(currentMode == true)
			showResultBooks();
		else 
			showResultUsers();	
	}
	
	/**
	 * @wbp.parser.entryPoint
	 */
	private void showResultBooks() {
		
		String num = messageDom.getElementsByTagName("num").item(0).getFirstChild().getNodeValue();
		int n = Integer.parseInt(num);
		mediaArray = new Media[n];
		for(int i=1; i<=n; i++) {
			int j = i - 1;
			String title = messageDom.getElementsByTagName("title" + i).item(0).getFirstChild().getNodeValue();
			String author = messageDom.getElementsByTagName("author" + i).item(0).getFirstChild().getNodeValue();
			String author2 = messageDom.getElementsByTagName("author2_" + i).item(0).getFirstChild().getNodeValue();
			String genre = messageDom.getElementsByTagName("genre" + i).item(0).getFirstChild().getNodeValue();
			String theme1 = messageDom.getElementsByTagName("theme1_" + i).item(0).getFirstChild().getNodeValue();
			String theme2 = messageDom.getElementsByTagName("theme2_" + i).item(0).getFirstChild().getNodeValue();
			String age = messageDom.getElementsByTagName("age" + i).item(0).getFirstChild().getNodeValue();
			String price = messageDom.getElementsByTagName("price" + i).item(0).getFirstChild().getNodeValue();
			String place = messageDom.getElementsByTagName("place" + i).item(0).getFirstChild().getNodeValue();
			String publisher = messageDom.getElementsByTagName("publish" + i).item(0).getFirstChild().getNodeValue();
			String info = messageDom.getElementsByTagName("info" + i).item(0).getFirstChild().getNodeValue();
			String material = messageDom.getElementsByTagName("material" + i).item(0).getFirstChild().getNodeValue();
			String avaible = messageDom.getElementsByTagName("avaible" + i).item(0).getFirstChild().getNodeValue();
			mediaArray[j] = new Media(title, author, author2, genre, theme1, theme2, age, price, place, publisher, info, material, avaible);
		}
		
		GWT.log("mediaArray full");
		vp = new VerticalPanel();
		vp.setSize("100%", "100%");
		
		cellTable = new CellTable<Media>();
		vp.add(cellTable);
		cellTable.setSize("100%", "100%");
		
		titleColumn = new Column<Media, String>(new EditTextCell()) {
			@Override
			public String getValue(Media object) {
				return object.getTitle();
			}
		};
		cellTable.addColumn(titleColumn, "Titel");
			
		authorColumn = new Column<Media, String>(new EditTextCell()) {
			@Override
			public String getValue(Media object) {
				return object.getAuthor();
			}
		};
		cellTable.addColumn(authorColumn, "Auteur");
		
		author2Column = new Column<Media, String>(new EditTextCell()) {
			@Override
			public String getValue(Media object) {
				return object.getAuthor2();
			}
		};
		cellTable.addColumn(author2Column, "Auteur2");
		
		genreColumn = new Column<Media, String>(new EditTextCell()) {
			@Override
			public String getValue(Media object) {
				return object.getGenre();
			}
		};
		cellTable.addColumn(genreColumn, "Genre");
		
		theme1Column = new TextColumn<Media>() {
			@Override
			public String getValue(Media object) {
				return object.getTheme1();
			}
		};
		cellTable.addColumn(theme1Column, "Thema1");
		
		theme2Column = new TextColumn<Media>() {
			@Override
			public String getValue(Media object) {
				return object.getTheme2();
			}
		};
		cellTable.addColumn(theme2Column, "Thema2");
		
		ageColumn = new TextColumn<Media>() {
			@Override
			public String getValue(Media object) {
				return object.getAge();
			}
		};
		cellTable.addColumn(ageColumn, "Age");
		
		priceColumn = new TextColumn<Media>() {
			@Override
			public String getValue(Media object) {
				return object.getPrice();
			}
		};
		cellTable.addColumn(priceColumn, "Price");
		
		placeColumn = new TextColumn<Media>() {
			@Override
			public String getValue(Media object) {
				return object.getPlace();
			}
		};
		cellTable.addColumn(placeColumn, "Place");
		
		simplePager = new SimplePager();
		vp.add(simplePager);
		simplePager.setWidth("100%");
		simplePager.setDisplay(cellTable);
		
		mediaList = Arrays.asList(mediaArray);
		
		ListDataProvider<Media> dataProvider = new ListDataProvider<Media>();
		dataProvider.addDataDisplay(cellTable);
		
		List<Media> list = dataProvider.getList();
		for(Media media : mediaList) {
			list.add(media);
		}	
	}
	
	private void showResultUsers() {
		// TODO Auto-generated method stub
		
	}
}
